package com.tutu.service.impl;

import com.tutu.controller.backend.EmissionCodeController;
import com.tutu.core.Constant;
import com.tutu.core.api.EmissionCodeApiUrls;
import com.tutu.entity.vo.BaseCodeVo;
import com.tutu.service.DeviceService;
import com.tutu.service.EmissionCodeService;
import com.tutu.utils.HttpRequestUtil;
import com.tutu.utils.JsonUtil;
import com.tutu.utils.UnicodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * EmissionCodeServiceImpl
 */
@Service
@Slf4j
public class EmissionCodeServiceImpl implements EmissionCodeService {

    @Autowired
    DeviceService deviceService;

    @Override
    public Map<String, Object> pageByParams(Integer type, String keyWord, Integer pageNum, Integer pageSize) {
        return HttpRequestUtil.getOnlyData(EmissionCodeApiUrls.base_prefix + EmissionCodeApiUrls.page, "keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize + "&type=" + type);
    }

    @Override
    public List<BaseCodeVo> findListByType(Integer type) {
        List<BaseCodeVo> list = new ArrayList<>();

        for (Map<String, Object> map : EmissionCodeController.emissionCodeList)
            if (type.equals(Integer.parseInt(map.get("type") + ""))) list.add(new BaseCodeVo(Integer.parseInt(map.get("id") + ""), "", map.get("content") + ""));

        return list;
    }

    @Override
    public List<BaseCodeVo> findListByDeviceId(Integer deviceId) {
        List<BaseCodeVo> list = new ArrayList<>();

        Map<String, Object> deviceMap = deviceService.getInfoById(deviceId);

        if (deviceMap.size() > 0) for (Map<String, Object> map : EmissionCodeController.emissionCodeList)
            if (Integer.parseInt(deviceMap.get("deviceType") + "") == (Integer.parseInt(map.get("type") + ""))) list.add(new BaseCodeVo(Integer.parseInt(map.get("id") + ""), "", map.get("content") + ""));

        return list;
    }

    @Override
    public Map<String, Object> saveInfo(HttpServletRequest request, Integer id, Integer type, String content) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑排放因子信息，ID：" + id + "，类型：" + type + "，名称：" + content);

        Map<String, Object> map = HttpRequestUtil.getCompleteData(EmissionCodeApiUrls.base_prefix + EmissionCodeApiUrls.update, "id=" + id + "&type=" + type + "&content=" + UnicodeUtil.unicodeEncode(content));

        if (map.get("code").equals(Constant.CHECK_STATUS_DEFAULT)) loadCacheData();

        return map;
    }

    @Override
    public Map<String, Object> deleteById(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "删除排放因子信息，ID：" + id);

        Map<String, Object> map = HttpRequestUtil.getCompleteData(EmissionCodeApiUrls.base_prefix + EmissionCodeApiUrls.delete, "id=" + id);

        if (map.get("code").equals(Constant.CHECK_STATUS_DEFAULT)) loadCacheData();

        return map;
    }

    @Override
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        LogInfoServiceImpl.saveLogInfo(request, "批量删除排放因子信息，IDS：" + ids);

        Map<String, Object> map = HttpRequestUtil.getCompleteData(EmissionCodeApiUrls.base_prefix + EmissionCodeApiUrls.batchDel, "ids=" + ids);

        if (map.get("code").equals(Constant.CHECK_STATUS_DEFAULT)) loadCacheData();

        return map;
    }

    @Override
    public void loadCacheData() {
        EmissionCodeController.emissionCodeList.clear();

        for (Object o : HttpRequestUtil.getListData(EmissionCodeApiUrls.base_prefix + EmissionCodeApiUrls.list, ""))
            EmissionCodeController.emissionCodeList.add(JsonUtil.json2Map(o));

        log.info("共计加载" + EmissionCodeController.emissionCodeList.size() + "条排放因子信息...");
    }
}